package edu.cs.usask.ca.silver.plugin.rcp.views.projectviewer;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.MessageDialog;

import edu.cs.usask.ca.silver.api.persistence.StorageI;
import edu.cs.usask.ca.silver.exceptions.UnableToInitializeStorageException;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverTreeObject;
import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;

public class CustomProjectView extends AbstractProjectView {

	public static final String ID = "edu.cs.usask.ca.silver.plugin.rcp.customprojectview";
	private static Logger logger = Logger.getLogger(CustomProjectView.class);

	@Override
	protected void initializeAssociatedStorageAndTreeRootObject() {
		if (SilverBusinessLogicController
				.checkIfHasNoDatabaseConnectionInPreference()) {
			try {
				this.storage = SilverBusinessLogicController
						.createRemoteDerbyStorageFromDatabasePreference();
				this.storage.initializeStorage();
				this.userid = SilverBusinessLogicController
						.fetchUserIDfromDatabaseByPreferenceInfo(this.storage);
				if (this.userid != null)
					silverTreeRoot = new SilverTreeObject();
			} catch (UnableToInitializeStorageException ste) {
				LoggerReportScheduler.scheduleErrorLogging(logger,
						"Failed to initialize storage in CustomProjectView at startup", ste);
				this.invalidate();
			}
			catch (Exception e1) {
				LoggerReportScheduler.scheduleErrorLogging(logger,
						"error in CustomProjectView", e1);
			}
		}
		startMonitorDatabaseJob(ID, 4);
	}

	@Override
	public void resetAssociatedStorage(StorageI databaseInstance) {
		if (databaseInstance == null)
			return;

		this.storage = databaseInstance;

		try {
			this.storage.initializeStorage();
			this.userid = SilverBusinessLogicController
					.fetchUserIDfromDatabaseByPreferenceInfo(this.storage);
			this.silverTreeRoot = new SilverTreeObject();
			this.treeViewer = tree.getViewer();
			this.getSite().setSelectionProvider(this.treeViewer);
			this.treeViewer.setLabelProvider(new ProjectViewLabelProvider(
					this.storage));
			this.treeViewer.setContentProvider(new ProjectViewContentProvider(
					this.storage, this.silverTreeRoot));
			this.treeViewer.setInput(this.silverTreeRoot);
			this.treeViewer.refresh();

		} catch (Exception e) {
			MessageDialog
					.openError(
							this.getSite().getShell(),
							"Unable to connect to database",
							"Unable to connect to the remote database.\n It is likely due to lack of Internet access, or remote database is unreachable.\n To try again, please check your connection specification carefully.");
			this.invalidate();
		}
	}

	@Override
	public void resetAssociatedUserToPreferenceInformation() {
		try {
			this.storage.initializeStorage();
			this.userid = SilverBusinessLogicController
					.fetchUserIDfromDatabaseByPreferenceInfo(this.storage);
			this.silverTreeRoot = new SilverTreeObject();
		} catch (Exception e) {
			LoggerReportScheduler.scheduleErrorLogging(logger,
					"error in CustomProjectView", e);
		} finally {

			this.treeViewer = tree.getViewer();
			this.getSite().setSelectionProvider(this.treeViewer);
			this.treeViewer.setLabelProvider(new ProjectViewLabelProvider(
					this.storage));
			this.treeViewer.setContentProvider(new ProjectViewContentProvider(
					this.storage, this.silverTreeRoot));
			this.treeViewer.setInput(this.silverTreeRoot);
			this.treeViewer.refresh();
		}

	}

}
